package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.BindUnbindGroupRoleCommand;
import com.wiscamp.ninechapters.panels.application.command.BindUnbindGroupUserCommand;
import com.wiscamp.ninechapters.panels.application.command.CreateGroupCommand;
import com.wiscamp.ninechapters.panels.application.command.UpdateGroupCommand;
import com.wiscamp.ninechapters.panels.application.services.GroupAppService;
import com.wiscamp.ninechapters.panels.application.vo.GroupVO;
import com.wiscamp.ninechapters.panels.application.vo.RoleVO;
import com.wiscamp.ninechapters.panels.application.vo.UserVO;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 用户组服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-用户组信息")
public class GroupController extends BaseController {

    private final GroupAppService groupAppService;

    public GroupController(GroupAppService groupAppService) {
        this.groupAppService = groupAppService;
    }

    @GetMapping("Groups/{groupId}")
    @Operation(summary = "通过编号获取用户组")
    @HandleResourceException
    public RestResponse<GroupVO> getGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                          @PathVariable("groupId") long groupId) {
        return ResourceUtils.getById(request, response, groupId, groupAppService::getGroup);
    }


    @GetMapping("Applications/{appId}/Groups/Code/{groupCode}")
    @Operation(summary = "通过代码获取用户组")
    @HandleResourceException
    public RestResponse<GroupVO> getGroupByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                @PathVariable("appId") long appId, @PathVariable("groupCode") String groupCode) {
        return ResourceUtils.getByIdAndText(request, response, appId, groupCode, groupAppService::getGroupByCode);
    }

    @GetMapping("Applications/{appId}/Groups/Name/{groupName}")
    @Operation(summary = "通过名称获取用户组列表")
    @HandleResourceException
    public RestResponse<Collection<GroupVO>> getGroupsByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                             @PathVariable("appId") long appId, @PathVariable("groupName") String groupName) {
        return ResourceUtils.getListByIdAndText(request, response, appId, groupName, groupAppService::getGroupsByName);
    }

    @PostMapping("Groups/Query")
    @Operation(summary = "分页获取用户组列表")
    @HandleResourceException
    public RestResponse<Collection<GroupVO>> getGroupsByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                              @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, groupAppService::getGroupsByQuery);
    }

    @GetMapping("Groups/{groupId}/Users")
    @Operation(summary = "通过编号获取用户组关联的用户")
    @HandleResourceException
    public RestResponse<Collection<UserVO>> getUsersByGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @PathVariable("groupId") long groupId) {
        return ResourceUtils.getById(request, response, groupId, groupAppService::getUsersByGroup);
    }

    @GetMapping("Groups/{groupId}/Roles")
    @Operation(summary = "通过编号获取用户组关联的角色")
    @HandleResourceException
    public RestResponse<Collection<RoleVO>> getRolesByGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @PathVariable("groupId") long groupId) {
        return ResourceUtils.getById(request, response, groupId, groupAppService::getRolesByGroup);
    }

    @PostMapping("Groups")
    @Operation(summary = "创建用户组")
    @HandleResourceException
    public RestResponse<GroupVO> createGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreateGroupCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, groupAppService::createGroup);
    }


    @PutMapping("Groups")
    @Operation(summary = "更新用户组")
    @HandleResourceException
    public RestResponse<GroupVO> updateGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdateGroupCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, groupAppService::updateGroup);
    }

    @PutMapping("Groups/Actions/Delete")
    @Operation(summary = "通过编号删除用户组")
    @HandleResourceException
    public RestResponse<Boolean> deleteGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, groupAppService::deleteGroup);
    }

    @PutMapping("Groups/Actions/Enable")
    @Operation(summary = "通过编号启用用户组")
    @HandleResourceException
    public RestResponse<Boolean> enableGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, groupAppService::enableGroup);
    }

    @PutMapping("Groups/Actions/Disable")
    @Operation(summary = "通过编号禁用用户组")
    @HandleResourceException
    public RestResponse<Boolean> disableGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, groupAppService::disableGroup);
    }


    @PostMapping("Groups/Actions/Bind/User")
    @Operation(summary = "绑定用户组用户关系")
    @HandleResourceException
    public RestResponse<Boolean> bindGroupUser(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody BindUnbindGroupUserCommand command) {
        var success = groupAppService.bindGroupUser(command);
        return ResourceUtils.getOkResponse(request, response, success);
    }

    @PostMapping("Groups/Actions/Unbind/User")
    @Operation(summary = "解绑用户组用户关系")
    @HandleResourceException
    public RestResponse<Boolean> unbindGroupUser(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody BindUnbindGroupUserCommand command) {
        var success = groupAppService.unbindGroupUser(command);
        return ResourceUtils.getOkResponse(request, response, success);
    }

    @PostMapping("Groups/Actions/Bind/Role")
    @Operation(summary = "绑定用户组角色关系")
    @HandleResourceException
    public RestResponse<Boolean> bindGroupRole(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody BindUnbindGroupRoleCommand command) {
        var success = groupAppService.bindGroupRole(command);
        return ResourceUtils.getOkResponse(request, response, success);
    }

    @PostMapping("Groups/Actions/Unbind/Role")
    @Operation(summary = "解绑用户组角色关系")
    @HandleResourceException
    public RestResponse<Boolean> unbindGroupRole(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody BindUnbindGroupRoleCommand command) {
        var success = groupAppService.unbindGroupRole(command);
        return ResourceUtils.getOkResponse(request, response, success);
    }
}
